/**
 * Copyright (C) 2012 GreenSoft S.A.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ar.com.greensoft.neosystem.model.address;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.persistence.Cacheable;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

import ar.com.greensoft.neosystem.model.NamedEntity;

/**
 * @author Jorge E. Villaverde
 * @version 1.0.0
 * @date 24/06/2011  
 */
@Entity
@Table(name="provincia")
@Cacheable(value=false)
@Inheritance(strategy=InheritanceType.JOINED)
@NamedQueries({
	@NamedQuery(name=Provincia.FIND_ALL_QUERY_NAME, 
		query="SELECT p FROM Provincia p ORDER BY p.name"),
	@NamedQuery(name=Provincia.FIND_LIKE_NAME_QUERY_NAME, 
		query="SELECT p FROM Provincia p WHERE p.name LIKE :name"),
	@NamedQuery(name=Provincia.FIND_BY_PAIS_NAME_QUERY_NAME, 
		query="SELECT p FROM Provincia p WHERE p.pais = :pais AND p.name = :name"),
	@NamedQuery(name=Provincia.FIND_BY_NAME_QUERY_NAME, 
		query="SELECT p FROM Provincia p WHERE p.name = :name"),
	@NamedQuery(name=Provincia.FIND_BY_PAIS, 
		query="SELECT p FROM Provincia p WHERE p.pais = :pais ORDER BY p.name"),
	@NamedQuery(name=Provincia.COUNT_QUERY_NAME, 
		query="SELECT COUNT(p) FROM Provincia p")
})
public class Provincia extends NamedEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6623675667778173815L;
	
	public static final String FIND_LIKE_NAME_QUERY_NAME = "Provincia.findNameLike";
	public static final String FIND_ALL_QUERY_NAME = "Provincia.findAll";
	public static final String FIND_BY_NAME_QUERY_NAME = "Provincia.findByName";
	public static final String FIND_BY_PAIS = "Provincia.findByPais";
	public static final String COUNT_QUERY_NAME = "Provincia.count";
	public static final String FIND_BY_PAIS_NAME_QUERY_NAME = "Provincia.findByPaisName";


	@NotNull
	@ManyToOne
	@JoinColumn(name = "ID_PAIS")
	private Pais pais;

    @OneToMany(mappedBy = "provincia", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
    private List<Localidad> localidades = Collections.synchronizedList(new ArrayList<Localidad>()); 	
	
	public Pais getPais() {
		return pais;
	}

	public void setPais(Pais pais) {
		this.pais = pais;
	}

	public List<Localidad> getLocalidades() {
		return localidades;
	}

	public void setLocalidades(List<Localidad> localidades) {
		this.localidades = localidades;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result
				+ ((localidades == null) ? 0 : localidades.hashCode());
		result = prime * result + ((pais == null) ? 0 : pais.hashCode());
		return result;
	}
}